<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <el-input v-model="code" ref="code" placeholder="请刷受试者条码" size="mini" @keyup.native="examineeNameKeyup($event)"
          style="width: 120px; margin-right: 10px"></el-input>
        <el-input v-model="examineeName" placeholder="请输入姓名" size="mini"
          style="width: 120px; margin-right: 10px"></el-input>
        <el-select v-model="sex" size="mini" clearable style="width: 120px; margin-right: 10px">
          <el-option :value="item.value" :label="item.label" v-for="(item, index) in sexList"
            v-bind:key="index"></el-option>
        </el-select>
        <!-- <el-select
          v-model="status"
          size="mini"
          style="width: 120px; margin-right: 10px"
        >
          <el-option value="" label="请选择状态"></el-option>
          <el-option value="1" label="状态1"></el-option>
          <el-option value="0" label="状态2"></el-option>
        </el-select> -->
        <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
        <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>

        <custom-button type="custom" style="margin-right: 10px" title="签署知情" color="#08A362" @customClick="customClick"
          icon="el-icon-edit"></custom-button>
        <custom-button type="custom" style="margin-right: 10px" icon="el-icon-upload2" color="#08A362" title="上传知情扫描件"
          @customClick="customClick"></custom-button>
        <!--				<custom-button type="custom" icon="el-icon-question" color="#1684FC" title="提问和答疑"-->
        <!--					@customClick="customClick"></custom-button>-->
      </div>
    </navigation-bar>
    <table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" btnListWidth="160px"
      :pageSize="pageSize" :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @lookSMJRow="lookSMJRow"
      isSizes @viewRow="viewRow" @editRow="editRow" :selection="true" @handleSizeChange="handleSizeChange"
      @handleSelectionChange="handleSelectionChange"></table-index>

    <div v-if="dialogShow">
      <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
        <form-index :formConfig="formConfig" :type="type" ref="form" @submit="submit" :nidShow="nidShow">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
    <div v-if="lmjDialogShow">
      <custom-dialog title="查看扫描件" size="small" @dialogClose="lmjDialogClose">
        <look-scan :ImgBase64List="[
          require('@/assets/images/smj/smj.jpg'),
          require('@/assets/images/smj/smj2.jpg'),
        ]"></look-scan>
      </custom-dialog>
    </div>

    <div v-if="smjDialogShow">
      <custom-dialog title="上传知情扫描件" size="small" @dialogClose="smjDialogClose">
        <upload-file @uploadFileHandle="uploadFileHandle"></upload-file>
        <div style="margin-top: 10px; text: right; text-align: right; height: 40px">
          <el-button type="primary" @click.native.prevent="saveImg()" size="mini">确定</el-button>
        </div>
      </custom-dialog>
    </div>
  </div>
</template>

<script>
import { informedSignatureForm } from "@/assets/form/managementCenter/subjectManagement/filteringPeriodManagement/informedSignatureForm"
import customDialog from "@/components/customComponents/customDialog"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import lookScan from "@/components/customComponents/managementCenter/lookScan"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import uploadFile from "@/components/customComponents/managementCenter/uploadFile"
import { AjaxCommon, printAddress } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
import { Utility } from "@/libs/utility"

export default {
  components: {
    navigationBar,
    customButton,
    tableIndex,
    customDialog,
    formIndex,
    lookScan,
    uploadFile,
  },
  data() {
    return {
      nidShow: '',
      status: "",
      state:false,
      examineeName: "",
      sex: "",
      sexList: Utility.getDictList("sex"),
      lmjDialogShow: false,
      hisCodeShow: false,
      smjDialogShow: false,
      selectItem: [],
      // table 相关参数  begin
      dataSource: [],
      tableColumns: [
        // {
        // 	prop: "id",
        // 	label: "主键",
        // 	width: 120,
        // },
        {
          prop: "examineeCode",
          label: "受试者编码",
          width: 120,
        },
        {
          prop: "filterNo",
          label: "筛选号",
          width: 100,
        },
        {
          prop: "examineeName",
          label: "姓名",
          width: 120,
        },
        {
          prop: "sex",
          label: "性别",
          width: 80,
          dictName: "sex",
        },
        {
          prop: "missionalByName",
          label: "宣教人",
          width: 100,
        },
        {
          prop: "missionalBeginTime",
          label: "宣教开始时间",
          width: 170,
        },
        {
          prop: "missionalEndTime",
          label: "宣教结束时间",
          width: 170,
        },
        {
          prop: "sign",
          label: "是否签署",
          width: 100,
          dictName: "sign",
        },
        {
          prop: "signTime",
          label: "签署时间",
          width: 170,
        },
        {
          prop: "updateByName",
          label: "研究者",
        },
      ],
      btnList: ["edit", "view", "lookSMJ"],
      pageSize: 20,
      pageCount: 100,
      pageNo: 1,
      // table 相关参数  end

      // 弹窗是否显示
      dialogShow: false,
      // 弹窗标题 (新增修改不同)
      dialogTitle: "",

      // form的配置信息
      formConfig: informedSignatureForm,
      type: "add",
      fileLists: '',

      code: "",
    }
  },

  methods: {
    examineeNameKeyup(e) {
      if (e.target.value.length > 9 && !this.state) {
        let self = this
        self.state = true
        setTimeout(() => {
          self.state = false
          self.code = ''
        }, 1000)
        self.examineeCode = e.target.value

        let data
        // 查询记录列表
        for (let i = 0; i < self.dataSource.length; i++) {
          if (self.dataSource[i].examineeCode == self.examineeCode) {
            data = self.dataSource[i]
          }
        }
        console.log(data)
        if (!data) {
          PopupCommon.notify("error", '查无此人')
          return
        }


        let formConfigItem = this.formConfig
        formConfigItem.map((t) => {
          Object.keys(t).map((res) => {
            if (res == "fieldName") {
              if (t[res] == "examineeCode") {
                t.filedValue = data.examineeCode
              } else if (t[res] == "examineeName") {
                t.filedValue = data.examineeName
              } else if (t[res] == "id") {
                t.filedValue = data.id
              } else {
                t.filedValue = ""
              }
            }
          })
        })

        this.nidShow = 'show'
        this.formConfig = formConfigItem
        this.type = "edit"
        this.dialogShow = true
        this.dialogTitle = JSON.parse(
          SessionCommon.getCurrentMenuInfo()
        ).title

      }
    },
    handleSizeChange(e) {
      this.pageSize = e
      this.initPage()
    },
    uploadFileHandle(files) {
      this.fileLists = files
    },
    printClick(examineeCode) {
      let urlAdd = `${printAddress}/ccdc/tmrreport/pdf/show?1=1&_u=file:shoushizheV1.tmrreport.xml&_i=1&_r=1`
      urlAdd += "&examineeCode=" + examineeCode
      let projectTestperiod =
        window.sessionStorage.getItem("projectTestperiod")
      if (projectTestperiod) {
        projectTestperiod = JSON.parse(projectTestperiod)
        urlAdd += `&pmId=${projectTestperiod.projectId}&expId=${projectTestperiod.testId}`
      }
      console.log(urlAdd)
      window.open(urlAdd)
    },
    submit(d) {
      let self = this
      this.nidShow = ''
      console.log(d)
      if (d.id) {
        AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/sign", "post", d)
          .then((res) => {
            if (res.data.code == 200) {
              if ("1" === d.sign) {
                PopupCommon.confirm("是否打印该受试者标签").then(
                  () => {
                    self.printClick(d.examineeCode)
                  }).catch(() => { })
              }
              PopupCommon.notify("success", "操作成功")
              self.initPage()
              self.dialogClose()
            } else {
              PopupCommon.notify("error", res.data.message)
            }
          })
          .catch((err) => {
            PopupCommon.notify("error", err.message)
          })
      } else {
        delete d.id
        AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/sign", "post", d)
          .then((res) => {
            if (res.data.code == 200) {
              if ("1" === d.sign) {
                PopupCommon.confirm("是否打印该受试者标签").then(
                  () => {
                    self.printClick(d.examineeCode)
                  }).catch(() => { })
              }
              PopupCommon.notify("success", "操作成功")
              self.initPage()
              self.dialogClose()
            } else {
              PopupCommon.notify("error", res.data.message)
            }
          })
          .catch((err) => {
            PopupCommon.notify("error", err.message)
          })
      }
    },
    handleSelectionChange(d) {
      this.selectItem = d
      console.log(d)
    },
    customClick(d) {
      if (d == "签署知情") {
        this.nidShow = 'show'
        if (this.selectItem.length != 1) {
          PopupCommon.notify("info", "请选择一个受试者进行签署")
          return
        }
        let formConfigItem = this.formConfig
        formConfigItem.map((t) => {
          Object.keys(t).map((res) => {
            if (res == "fieldName") {
              if (t[res] == "examineeCode") {
                t.filedValue = this.selectItem[0].examineeCode
              } else if (t[res] == "examineeName") {
                t.filedValue = this.selectItem[0].examineeName
              } else if (t[res] == "id") {
                t.filedValue = this.selectItem[0].id
              } else {
                t.filedValue = ""
              }
            }
          })
        })
        this.formConfig = formConfigItem
        console.log(this.selectItem, this.formConfig)
        this.type = "edit"
        this.dialogShow = true
        this.dialogTitle = JSON.parse(
          SessionCommon.getCurrentMenuInfo()
        ).title
      } else if (d == "上传知情扫描件") {
        if (this.selectItem.length != 1) {
          PopupCommon.notify("info", "仅勾选一个受试者进行操作")
          return
        }
        this.smjDialogShow = true
      } else {
        // 疑问
        PopupCommon.notify("success", "当前选中的成员，已经完成知情操作！")
      }
    },
    dialogClose() {
      this.dialogShow = false
      this.nidShow = ''
    },
    lmjDialogClose() {
      this.lmjDialogShow = false
    },
    smjDialogClose() {
      this.smjDialogShow = false
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.examineeName = ""
      this.sex = ""
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    lookSMJRow(d) {
      console.log(d)
      console.log("暂无扫描件字段，搞张示意图片。")
      this.lmjDialogShow = true
    },
    editRow(d) {
      this.type = "edit"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            t.filedValue = d.data[t[res]]
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
        "编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    viewRow(d) {
      this.type = "look"
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == "fieldName") {
            t.filedValue = d.data[t[res]]
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
        "查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    saveImg() {
      let _this = this;
      AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/img", "post", {
        id: _this.selectItem[0].id,
        attachmentUrl: _this.fileLists,
      }).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
          _this.smjDialogClose()
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
    // 初始化事件
    initPage() {
      let self = this
      AjaxCommon("/ccdc/pm/em/filter/emFilterMissional/listByPage", "post", {
        examineeName: this.examineeName,
        examineeCode: this.examineeCode,
        status: 'end',
        sex: this.sex,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.data.code == 200) {
          self.code = ""
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
  },
  mounted() {
    this.initPage()
    this.$refs.code.focus()
  },
}
</script>

<style></style>
